package com.bupt.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * 用来解决feign调用不携带session问题
 */
public class FeignClientsConfigurationCustom implements RequestInterceptor {

//  @Bean
//  public FeignHystrixConcurrencyStrategy feignHystrixConcurrencyStrategy() {
//    return new FeignHystrixConcurrencyStrategy();
//  }

  @Override
  public void apply(RequestTemplate template) {
    ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
            .getRequestAttributes();
    assert attributes != null;
    HttpServletRequest request = attributes.getRequest();
    Enumeration<String> headerNames = request.getHeaderNames();
    if (headerNames != null) {
      while (headerNames.hasMoreElements()) {
        String name = headerNames.nextElement();
        String values = request.getHeader(name);
        // 跳过 content-length
        // 否则可能报错too many bytes written
        if (name.equals("content-length")){
          continue;
        }
        template.header(name, values);
      }
    }
  }


//  }
 
} 